<?php
/**
 * 用户管理
 *
 * @author karl <karl.tang@aiyuangong.com>
 * @link http://www.aiyuangong.com
 */

namespace app\admin\controller\member;

use app\admin\Controller;
use think\Lang;
use think\Loader;
use think\Exception;
use think\Request;
use think\Config;

class Member extends Controller
{
	use \app\admin\traits\controller\Controller;
	protected static $blacklist = ['deleteforever', 'clear'];
    
    /**
     * 通知会员页面
     */
    public function notify(){
    	$id = $this->request->param('id/d');
    	if($id){
    		//会员信息
    		$obj = Loader::model('Member');
    		$member_info = Loader::model('Member')->getMember('id, name', ['id'=>$id]);
    		$this->view->assign('member', $member_info->getData());
    	}
    	return $this->view->fetch();
    }
    
    /**
     * 发送通知
     */
    public function send_notify(){
        //提交
        if ($this->request->isPost()) {
            $send_type = $this->request->param('send_type/d',1);//发送给谁，1为指定会员，2为全部会员
            $user_name = $this->request->param('user_name/s','0');
            $content = $this->request->param('content/s','0');
            //验证
            switch ($send_type) {
                //指定会员
                case 1:
                    if(empty($user_name) || empty($content)){
                        return ajax_return_adv_error('请填写会员列表和通知内容');
                    }
                    break;
                //全部会员
                case 2:
                    if(empty($content)){
                        return ajax_return_adv_error('请填写会员列表和通知内容');
                    }
                    break;
            } 
            //发送会员ID 数组
            $memberid_list = array();
            //指定会员
            if ($send_type == 1) {
                $model_member = model('common/member');
                $tmp = explode("\n", $user_name);
                if (!empty($tmp)) {
                    foreach ($tmp as $k => $v) {
                        $tmp[$k] = trim($v);
                    }
                    //查询会员列表
                    $member_list = $model_member->getMemberList(['name'=>['in', $tmp]],'id');
                    unset($membername_str);
                    if (!empty($member_list)) {
                        foreach ($member_list as $k => $v) {
                            $memberid_list[] = $v['id'];
                        }
                    }
                    unset($member_list);
                }
                unset($tmp);
            }
            if (empty($memberid_list) && $send_type != 2) {
                return ajax_return_adv_error('通知失败');
            }
            
            //添加系统消息
            $model_message = model('common/message');
            $insert_arr = [];
            $insert_arr['from_member_id'] = 0;
            if ($send_type == 2) {
                $insert_arr['member_id'] = 'all';
            } else {
                $insert_arr['member_id'] = ',' . implode(',', $memberid_list) . ',';
            }
            $insert_arr['msg_content'] = $content;
            $insert_arr['message_type'] = 1;
            $insert_arr['message_ismore'] = 1;
            $reutl = $model_message->saveMessage($insert_arr);
            return ajax_return_adv('通知成功', '');
        }
        return ajax_return_adv_error('通知失败');
    }
    
    /**
     * 会员等级页面
     */
    public function grade(){
    	//会员信息
    	$gradeArr = Config::get('member.grade');
    	$this->view->assign('grade',$gradeArr);
    	return $this->view->fetch();
    }
    
    /**
     * 保存会员等级
     */
    public function grade_save(){
    	$grade = $this->request->post('grade/a');
    	//等级信息
    	$gradeArr = Config::set('member.grade',$grade);
    	return ajax_return_adv('成功', '');
    }
}